package com.mymoney.resources;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.mymoney.beans.PlatformBean;
import com.mymoney.persistence.PersistenceContext;
import com.mymoney.persistence.managers.PlatformManager;
import com.mymoney.utils.ServiceUtils;

@Path("/platforms")
public class PlatformResource extends AbstractWebResource {
	
	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	public Response list() {
		Response response = null;
		PersistenceContext persistenceContext = PersistenceContext.getPersistenceContext();
		
		try
		{
			if(persistenceContext != null)
			{
				PlatformManager platformManager = persistenceContext.getPlatformManager();
				List<PlatformBean> platformsList = platformManager.list();
				response = Response.ok(platformsList).type(MediaType.APPLICATION_JSON).build();
			}
			else
			{
				throw new NullPointerException();
			}
		}catch (Exception e) {
			response = ServiceUtils.getErrorResponse(111, e);
		}
		
		return response;
	}
}
